﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Amib.Threading;

namespace RRDocumentSync.Console
{
    public class Program
    {
        public delegate void EventHandler(object sender, DataHandled data);

        public class DataHandled
        {
            public int No { get; set; }
            public int MillisecondsExecution { get; set; }
        }

        public class A : IDisposable
        {
            private string privateS;
            protected string protectedS;

            public A()
            {
                System.Console.WriteLine("A default constructor");
            }

            public A(string s)
                : this()
            {
                this.privateS = string.Format("A private S : {0}", s);
                this.protectedS = string.Format("A protected S : {0}", s);

                System.Console.WriteLine("A custom constructor : {0}", s);
            }

            public event EventHandler EventHandler;

            public IEnumerable<DataHandled> ListOfData { get; set; }

            ~A()
            {
                System.Console.WriteLine("A destructor");
                this.Dispose();
            }

            public virtual void Dispose()
            {
                System.Console.WriteLine("A is disposed");
                System.Console.WriteLine(privateS);
                System.Console.WriteLine(protectedS);
            }

            public virtual void Execution()
            {
                foreach (var data in this.ListOfData)
                {
                    if (this.EventHandler != null)
                    {
                        this.EventHandler(this, data);
                    }
                }
            }
        }

        public class B : A
        {
            private string privateS;

            public B()
            {
                System.Console.WriteLine("B default constructor");
            }

            public B(string s)
                : base(s)
            {
                this.privateS = string.Format("B private S : {0}", s);
                this.protectedS = string.Format("B protected S : {0}", s);

                System.Console.WriteLine("B custom constructor : {0}", s);
            }

            ~B()
            {
                System.Console.WriteLine("B destructor");
            }

            public override void Dispose()
            {
                System.Console.WriteLine("B is disposed");
                System.Console.WriteLine(privateS);
                System.Console.WriteLine(protectedS);
                base.Dispose();
            }
        }

        public static void Main(string[] args)
        {
            DateTime time = DateTime.Now;
            string timeString = time.ToString();
            try
            {
                DateTime newTime = DateTime.Parse(timeString);
            }
            catch (Exception ex)
            {

            }

            //Method();
        }

        private static void Method()
        {
            if (true)
            {
                using (var b = new B("Siapa Aku?"))
                {
                    b.EventHandler += EventHandlerMethod;
                    b.ListOfData = new List<DataHandled>()
                    {
                        new DataHandled{ No = 1, MillisecondsExecution = 5000 },
                        new DataHandled{ No = 2, MillisecondsExecution = 10000 },
                        new DataHandled{ No = 3, MillisecondsExecution = 7000 },
                        new DataHandled{ No = 4, MillisecondsExecution = 2000 },
                        new DataHandled{ No = 5, MillisecondsExecution = 4000 },
                        new DataHandled{ No = 6, MillisecondsExecution = 9000 },
                        new DataHandled{ No = 7, MillisecondsExecution = 8000 },
                        new DataHandled{ No = 8, MillisecondsExecution = 3000 },
                        new DataHandled{ No = 9, MillisecondsExecution = 6000 },
                        new DataHandled{ No = 10, MillisecondsExecution = 1000 }
                    };

                    b.Execution();

                    while (!stp.IsShuttingdown) { }
                }
            }
        }

        private static SmartThreadPool stp = new SmartThreadPool(new STPStartInfo { PostExecuteWorkItemCallback = CallBack }) { MinThreads = 3, MaxThreads = 15 };

        private static void EventHandlerMethod(object sender, DataHandled data)
        {
            var result = stp.QueueWorkItem(EventHandlerExecution, data);
            stp.Start();
        }

        public static void CallBack(IWorkItemResult result)
        {
            System.Console.WriteLine("Result Call back : {0}", (result.State as DataHandled).No);

            if (stp.InUseThreads <= 1) stp.Shutdown();
        }

        static void stp_OnIdle(IWorkItemsGroup workItemsGroup)
        {
            stp.Shutdown();
        }

        private static void EventHandlerExecution(DataHandled data)
        {
            System.Threading.Thread.Sleep(data.MillisecondsExecution);
            System.Console.WriteLine("Data {0} was sleep for {1} milliseconds", data.No, data.MillisecondsExecution);
            //return data.No;
        }

        //private static void InitAzureConnection2()
        //{
        //    // "net.tcp://redraindev.cloudapp.net:10001/AuthorizedUserService";
        //    string authenticationServiceUrl = @"net.tcp://127.255.0.1:11001/AuthorizedUserService";
        //    string documentSyncServiceUrl = @"net.tcp://127.255.0.1:10101/RedViewDocumentSyncService";

        //    EndpointAddress endpointAddress = new EndpointAddress(authenticationServiceUrl);
        //    NetTcpBinding binding = new NetTcpBinding();
        //    binding.Security.Mode = SecurityMode.None;
        //    binding.Security.Message.ClientCredentialType = MessageCredentialType.None;

        //    EndpointAddress endpointAddress2 = new EndpointAddress(documentSyncServiceUrl);
        //    NetTcpBinding binding2 = new NetTcpBinding();
        //    binding2.Security.Mode = SecurityMode.None;
        //    binding2.Security.Message.ClientCredentialType = MessageCredentialType.None;

        //    // binding = new NetTcpBinding(SecurityMode.None);
        //    //ChannelFactory<IAuthorizedUserService> channelFactory = new ChannelFactory<IAuthorizedUserService>(binding, endpointAddress);
        //    //channelFactory.Endpoint.EndpointBehaviors.Add(new InterceptorBehaviorExtension());
        //    //IAuthorizedUserService authorizedUserChannel = channelFactory.CreateChannel();

        //    using (AuthorizedUserServiceClient clientService = new AuthorizedUserServiceClient(binding, endpointAddress))
        //    {
        //        //clientService.Endpoint.EndpointBehaviors.Add(new InterceptorBehaviorExtension());

        //        try
        //        {
        //            RedRain.Collaborate.Contract.Models.GetAuthenticationTicketRequest request = new RedRain.Collaborate.Contract.Models.GetAuthenticationTicketRequest() { TenantId = "tenantnewredview", Username = "afisher@redraincorp.com", Password = "@accel4me" };
        //            var response = clientService.GetAuthenticationTicket(request); //authorizedUserChannel.Login(new LoginRequest { TenantId = "tenantnewredview", Username = "afisher@redraincorp.com", Password = "@accel4me" }); var responseMessageProperty = (HttpResponseMessageProperty)

        //            using (RedViewDocumentSyncServiceClient docSyncService = new RedViewDocumentSyncServiceClient(binding2, endpointAddress2))
        //            {
        //                UploadBlobRequest uploadRequest = new UploadBlobRequest
        //                {
        //                    ContainerName = "testcontainer",
        //                    BlobName = ""
        //                };
        //            }
        //        }
        //        catch (Exception e)
        //        {
        //            throw e;
        //        }
        //    }
        //}

    }
}
